/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package cn.yzc.write;

import cn.yzc.pojo.Student;
import cn.yzc.utils.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;

import java.util.Date;
import java.util.List;

/**
 * <p>Project: EasyExcel - ManyWrite</p>
 * <p>Powered by yzc On 2024-03-20 17:01:01</p>
 * <p>描述：<p>
 *
 * @author yzc [youzhicheng0415@163.com]
 * @version 1.0
 * @since 17
 */
//批量写
public class ManyWrite {
    private static List<Student> data(int count){
        List<Student> list = ListUtils.newArrayList();
        for (int i = 1; i <= count; i++) {
            String gender = (i%2 == 0)?"女":"男";
            list.add(new Student(i,"学生"+i,gender,new Date()));
        }
        return list;
    }

    /**
     * 快速入门写数据
     */
    public static void main(String[] args) {
        // 方法2: 如果写到不同的sheet 同一个对象
       String fileName = TestFileUtil.getPath() + "repeatedWrite" + "批量百万数据"+ ".xlsx";
        // 这里 指定文件
        try (ExcelWriter excelWriter = EasyExcel.write(fileName, Student.class).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet("测试数据").build();
            for (int i = 1; i <= 100; i++) {
                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样

                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                List<Student> data = data(100);
                excelWriter.write(data, writeSheet);
            }
        }

    }
}
